//
//  NSDate+SinaDate.swift
//  iOSWeibo10
//
//  Created by 李伟鹏 on 16/5/19.
//  Copyright © 2016年 itcast. All rights reserved.
//

import Foundation

extension NSDate{
    
    class func date(sinaDateString: String) -> NSDate {
        let df = NSDateFormatter()
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        df.locale = NSLocale(localeIdentifier: "cn")
        let date = df.dateFromString(sinaDateString)
        return date ?? NSDate()
    }
    
    func sinaDateDescription() -> String{
        let calendar = NSCalendar.currentCalendar()
        if calendar.isDateInToday(self){
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            if delta < 60{
                // 一分钟内
                return "刚刚"
            }else if delta < 60 * 60{
                // 一小时内
                return "\(delta / 60)分钟前"
            }else{
                 // 当天
                return "\(delta / 60 / 60)小时前"
            }
        }
        
        let df = NSDateFormatter()
        var dateFomatter = ""
        if calendar.isDateInYesterday(self){
            dateFomatter = "昨天 HH:mm"
        }else if calendar.compareDate(self, toDate: NSDate(), toUnitGranularity: NSCalendarUnit.Year) == NSComparisonResult.OrderedSame{
            // self == 当前时间 // 同一年
            dateFomatter = "MM-dd HH:mm"
        }else {
            dateFomatter = "yyyy-MM-dd HH:mm"
        }
        df.dateFormat = dateFomatter
        return df.stringFromDate(self)
    }
    
}